package com.easyadmin.pro.modules.sys.controller;

import com.easyadmin.pro.common.constants.HttpConstants;
import com.easyadmin.pro.common.controller.BaseController;
import com.easyadmin.pro.common.vo.RestResult;
import com.easyadmin.pro.modules.sys.entity.RouterActionEntity;
import com.easyadmin.pro.modules.sys.service.IRouterActionService;
import com.easyadmin.pro.modules.sys.vo.RelevanceActionInfoVo;
import com.easyadmin.pro.modules.sys.vo.RouterActionInfoVo;
import com.easyadmin.pro.tool.BeanUtils;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 *  控制层。
 *
 * @author xulin
 * @since 2024-04-07
 */
@RestController
@RequestMapping(HttpConstants.SYS_MODULE_PATH + "/router/action/")
public class RouterActionController extends BaseController {

    @Autowired
    private IRouterActionService iRouterActionService;

    /**
     * 添加。
     *
     * @param routerActionEntity 
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    public RestResult save(@RequestBody RouterActionEntity routerActionEntity) {
        return success(iRouterActionService.save(routerActionEntity));
    }

    /**
     * 批量保存
     * @param routerActionInfos
     * @return
     */
    @PostMapping("batch")
    public RestResult batch(@RequestBody List<RouterActionInfoVo> routerActionInfos) {
        List<RouterActionEntity> saveInfos = BeanUtils.conventBeans(routerActionInfos, RouterActionEntity.class);
        return success(iRouterActionService.saveBatch(saveInfos));
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public RestResult remove(@PathVariable Serializable id) {
        return success(iRouterActionService.removeById(id));
    }

    /**
     * 根据主键更新。
     *
     * @param routerActionEntity 
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    public RestResult update(@RequestBody RouterActionEntity routerActionEntity) {
        return success(iRouterActionService.updateById(routerActionEntity));
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public RestResult list() {
        return success(iRouterActionService.list());
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    public RestResult getInfo(@PathVariable Serializable id) {
        return success(iRouterActionService.getById(id));
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public RestResult page(Page<RouterActionEntity> page) {
        return success(iRouterActionService.page(page));
    }

    /**
     * 获取路由关联按钮信息
     * @param routerId
     * @return
     */
    @GetMapping("getRouterActionInfo/{routerId}")
    public RestResult getRouterActionInfo(@PathVariable Long routerId) {
        return success(iRouterActionService.listAs(new QueryWrapper()
                .select(RouterActionEntity::getActionId).eq(RouterActionEntity::getRouterId, routerId), String.class));
    }

    /**
     * 路由关联按钮
     * @param relevanceActionInfoVo
     * @return
     */
    @PostMapping("saveRelevanceActionInfo")
    public RestResult saveRelevanceActionInfo(@RequestBody RelevanceActionInfoVo relevanceActionInfoVo) {
        iRouterActionService.saveRelevanceActionInfo(relevanceActionInfoVo);
        return success();
    }
}
